package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.MenuDao;
import com.baor.dao.RoleDao;
import com.baor.domain.MenuVo;
import com.baor.domain.RoleVo;
import com.baor.domain.UserVo;
import com.baor.service.MenuService;
import com.baor.utils.AjaxResult;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RoleDao roleDao;

    @Override
    public AjaxResult<?> menuList(int menuId) {
        LambdaQueryWrapper<MenuVo> lqw = new LambdaQueryWrapper<>();
        lqw.select(MenuVo.class,x -> !x.getColumn().equals("menu_superiors") && !x.getColumn().equals("role_id") && !x.getColumn().equals("menu_path"))
                .notIn(MenuVo::getMenuId, menuId);
        return AjaxResult.success(menuDao.selectList(lqw));
    }
    @Override
    public AjaxResult<?> menuList2() {
        LambdaQueryWrapper<MenuVo> lqw = new LambdaQueryWrapper<>();
        lqw.select(MenuVo.class,x -> !x.getColumn().equals("menu_superiors") && !x.getColumn().equals("role_id") && !x.getColumn().equals("menu_path"));
        return AjaxResult.success(menuDao.selectList(lqw));
    }

    @Override
    public AjaxResult<?> menu(int userId) {
        List<MenuVo> allMenus = menuDao.selectList(null);
        List<MenuVo> parentMenus = new ArrayList<>();
        List<Integer> parentId = menuDao.parentId(userId);
        System.out.println(parentId);
        for(Integer id : parentId){
            LambdaQueryWrapper<MenuVo> lqw = new LambdaQueryWrapper<>();
            lqw.eq(MenuVo::getMenuId, id);
            parentMenus.add(menuDao.selectOne(lqw));
        }
        System.out.println(parentMenus);
        for(MenuVo vo : parentMenus){
            List<MenuVo> childMenus = getChild(allMenus,vo.getMenuId());
            vo.setChildMenu(childMenus);
        }
        return AjaxResult.success(parentMenus);
    }
    public List<MenuVo> getChild(List<MenuVo> allMenu,int parentId){
        List<MenuVo> childMenu = new ArrayList<>();
        for(MenuVo vo : allMenu){
            if(vo.getMenuSuperiors().equals(String.valueOf(parentId))){
                childMenu.add(vo);
            }
        }
        for(MenuVo vo : childMenu){
            List<MenuVo> childVo = getChild(allMenu, vo.getMenuId());
            vo.setChildMenu(childVo);
        }
        if(childMenu.size() == 0){
            return null;
        }
        return childMenu;
    }
    @Override
    public AjaxResult<?> allMenu(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        List<MenuVo> list = menuDao.allMenu(param);
        map.put("menuList", list);
        map.put("pageCount", menuDao.pageCount(param));
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult<?> delete(int id) {
        int row = menuDao.deleteById(id);
        if(row > 0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> menuInfo(int id) {
        MenuVo vo = menuDao.menuInfo(id);
        return AjaxResult.success(vo);
    }

    @Override
    public AjaxResult<?> updateInfo(int id) {
        Map<String,Object> map = new HashMap<>();
        map.put("menuList", menuList(id).getData());
        map.put("menuInfo", menuInfo(id).getData());
        LambdaQueryWrapper<RoleVo> lqw = new LambdaQueryWrapper<>();
        lqw.select(RoleVo.class,x -> !x.getColumn().equals("role_superiors"));
        map.put("role", roleDao.selectList(lqw));
        return AjaxResult.success(map);
    }

    @Override
    public AjaxResult<?> update(Map<String, Object> param) {
        List<Integer> list = (List<Integer>) param.get("roleIds");
        int row2 = menuDao.deleteRole((Integer) param.get("menuId"));
        System.out.println(row2);
        for(Integer id : list){
            Map<String,Object> param1 = new HashMap<>();
            param1.put("roleId", id);
            param1.put("menuId", param.get("menuId"));
            System.out.println(param1);
            menuDao.insertRole(param1);
        }
        int row = menuDao.updata(param);
        if(row > 0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> addInfo() {
        Map<String,Object> map = new HashMap<>();
        map.put("menuList", menuList2().getData());
        LambdaQueryWrapper<RoleVo> lqw = new LambdaQueryWrapper<>();
        lqw.select(RoleVo.class,x -> !x.getColumn().equals("role_superiors"));
        map.put("role", roleDao.selectList(lqw));
        return AjaxResult.success(map);
    }

    @Override
    @Transactional
    public AjaxResult<?> insertMenu(Map<String, Object> param) {
        int row = menuDao.insertMenu(param);
        if(row > 0){
            List<Integer> list = (List<Integer>) param.get("roleIds");
            for(Integer id : list){
                Map<String,Object> param1 = new HashMap<>();
                param1.put("roleId", id);
                param1.put("menuId", param.get("menu_id"));
                menuDao.insertRole(param1);
            }
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }


}
